<?php

function dbconnect() 
	{ 
		require('config.php'); 
		static $connect = null; 
		if ($connect === null) 
			{
				 $connect =  mysql_connect($host, $user, $passwd) or die(mysql_error());
				 mysql_query("SET character_set_results=utf8", $connect);
				 mysql_select_db($database); 
			} 
		return $connect; 
	} 
	
	function getid_auto_increment()
			{
		
		$r= mysql_query("SHOW TABLE STATUS LIKE 'Bai_viet'",dbconnect());
		$row = mysql_fetch_array($r); 
        $n = $row['Auto_increment']; 
        return $n;
		
		} 
		
 function nbv() 
	{ 
		$kq_nvb = mysql_query("SELECT * FROM Nhom_bai_viet",dbconnect()); 
		$result = array(); 
		while ($term = mysql_fetch_array($kq_nvb)) { 
			$result[] = $term; 
		} 
		return $result; 
	} 
function getbyall_baiviet()
	{ 
		$allnxb_req = mysql_query("SELECT Bai_viet.*, Nhom_bai_viet.Ten_nhom_bai_viet, LEFT(Noi_dung, 200) as Noi_dung2 
		FROM `Bai_viet` 
		INNER JOIN Nhom_bai_viet 
		ON Nhom_bai_viet.ID = Bai_viet.ID_Nhom_bai_viet
		where Trang_thai=1 order by id desc limit 6" ,dbconnect()); 
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	}
function getbyall_baivietkm()
	{ 
		$allnxb_req = mysql_query("SELECT Bai_viet.*, Nhom_bai_viet.Ten_nhom_bai_viet, LEFT(Noi_dung, 200) as Noi_dung2 
		FROM `Bai_viet` 
		INNER JOIN Nhom_bai_viet 
		ON Nhom_bai_viet.ID = Bai_viet.ID_Nhom_bai_viet
		where (Trang_thai=1 and ID_Nhom_bai_viet=4) order by id desc limit 6" ,dbconnect()); 
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	}
function getbyall_baivietttc()
	{ 
		$allnxb_req = mysql_query("SELECT Bai_viet.*, Nhom_bai_viet.Ten_nhom_bai_viet, LEFT(Noi_dung, 900) as Noi_dung2 
		FROM `Bai_viet` 
		INNER JOIN Nhom_bai_viet 
		ON Nhom_bai_viet.ID = Bai_viet.ID_Nhom_bai_viet
		where (Trang_thai=1 and ID_Nhom_bai_viet=5) order by id desc limit 6" ,dbconnect()); 
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	}
function getbythoigian_baiviet($id)
	{ 
		$allnxb_req = mysql_query("SELECT * 
		FROM `Bai_viet` 
		where (Trang_thai=1 and Thoi_gian < N'".$id."')order by id desc limit 5" ,dbconnect()); 
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	}
function getbyid_baiviet($id)
	{ 
		$result = mysql_query("SELECT Bai_viet.*, Nhom_bai_viet.Ten_nhom_bai_viet, LEFT(Noi_dung, 200) as Noi_dung2 
		FROM `Bai_viet` 
		INNER JOIN Nhom_bai_viet 
		ON Nhom_bai_viet.ID = Bai_viet.ID_Nhom_bai_viet
		where (Trang_thai=1 and Bai_viet.ID=N'".$id."')  order by id desc limit 6" ,dbconnect()); 
		return mysql_fetch_array($result); 
	} 
function insert_bv1($bv, $img) 
			{ 
	 
		$query= mysql_query("INSERT INTO Bai_viet SET ID_Nhom_bai_viet=N'".$bv['ID_Nhom_bai_viet']."', Tieu_de=N'".$bv['Tieu_de']."',url_hinh_anh=N'".$img."',   Noi_dung=N'".$bv['Noi_dung']."', Thoi_gian=now(), Trang_thai='1' ", dbconnect());  
		
			}	
function insert_bv2($bv) 
			{ 
	 
		$query= mysql_query("INSERT INTO Bai_viet SET ID_Nhom_bai_viet=N'".$bv['ID_Nhom_bai_viet']."', Tieu_de=N'".$bv['Tieu_de']."',  Noi_dung=N'".$bv['Noi_dung']."',  Thoi_gian=now(), Trang_thai='2' ", dbconnect());  
		
			}	
function getbyid_bv($id)
	{
		$result= mysql_query("SELECT ID, Tieu_de, Thoi_gian, Noi_dung, ID_Nhom_bai_viet, Trang_thai FROM Bai_viet WHERE ID=N'".$id."'",dbconnect());
		return mysql_fetch_array($result); 
	}
function update_bv($bv)
	{
	$query= mysql_query("UPDATE Bai_viet SET ID_Nhom_bai_viet=N'".$bv['ID_Nhom_bai_viet']."', Tieu_de=N'".$bv['Tieu_de']."',  Noi_dung=N'".$bv['Noi_dung']."',  Trang_thai='1' WHERE ID=N'".$bv['ID']."' ",dbconnect()); 	
	}
function update_bv1($bv)
	{
	$query= mysql_query("UPDATE Bai_viet SET ID_Nhom_bai_viet=N'".$bv['ID_Nhom_bai_viet']."', Tieu_de=N'".$bv['Tieu_de']."',  Noi_dung=N'".$bv['Noi_dung']."',  Trang_thai='2' WHERE ID=N'".$bv['ID']."' ",dbconnect()); 	
	}	

function insert_binhluan($bv,$id) 
			{ 
	 
		$result= mysql_query("INSERT INTO Binh_luan_bai_viet 
		SET 
		Ten=N'".$bv['Ten']."', 
		Email=N'".$bv['Email']."',  
		Noi_dung=N'".$bv['Noi_dung']."',  
		Thoi_gian=now(), 
		Trang_thai='2',
		ID_Bai_viet=N'".$id."' ", dbconnect());  
		
		if ($result)
			{
		echo "<script> alert('Bình luận đã lưu, chúng tôi sẽ xem xét trước khi nó hiển thị công cộng!')</script>";
		

		mysql_close();

			}
			else
			{
			echo "<script> alert('Quá trình thêm dữ liệu bị lỗi!')</script>" . mysql_error();
			}
		
			}
function getbyid_binhluan($id)
	{ 
		$allnxb_req = mysql_query("SELECT *
		FROM `Binh_luan_bai_viet` 
		where (Trang_thai=1 and ID_Bai_viet=N'".$id."')  order by ID desc limit 6" ,dbconnect()); 
		$result = array(); 
		while ($nxb = mysql_fetch_array($allnxb_req))
		 { 
			$result[] = $nxb; 
		 } 
		return $result; 
	} 
?>